
package andre.grids.filesystem.client;

import andre.grids.guids.*;
import andre.grids.network.PeerObjects;
import andre.grids.util.PathUtils;
import java.io.FileOutputStream;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

/**
 *
 * @author andre
 */
public class RFile {
    private String fileName;
    private IGuid fileGuid;
    private RemoteServer remoteServer;
    
    public RFile(String file, RemoteServer server) throws RemoteException, NotBoundException {
        this.fileName = file;
        String parent = PathUtils.extractParentPath(this.fileName);
        if (!server.getDirOp().getDirectoryMaster(parent).equals(server.getHostName()))
            remoteServer = new RemoteServer(server.getDirOp().getDirectoryMaster(parent));
        else
            remoteServer = server;
    }
    
    public void create() throws RemoteException {
        this.fileGuid = remoteServer.getDirOp().createFile(fileName);
    }
    
    public void open() throws RemoteException {
        this.fileGuid = remoteServer.getDirOp().openFile(fileName);
    }
    
    public RemoteFileInputStream getInputStream() throws RemoteException {
        open();
        RemoteFileInputStream rfis = new RemoteFileInputStream(fileGuid, remoteServer.getFileOp());
        return rfis;
    }
    
    public RemoteFileOutputStream getOutpuStream() throws RemoteException {
        open();
        RemoteFileOutputStream rfos = new RemoteFileOutputStream(fileGuid, remoteServer.getFileOp());
        return rfos;
    }
    
}
